package service.impl;

import java.util.List;

import service.tUserService;
import dao.tUserDao;
import entity.PageBean;
import entity.tDevice;
import entity.tUser;

public class tUserServiceImpl implements tUserService {
	private tUserDao tuserDao;
	public tUser login(tUser user) {
		String queryString="from tUser user where user.userName='"+user.getUserName()+"' and user.pwd='"+user.getPwd()+"'";
		List<tUser> users=this.tuserDao.getTUser(queryString);
		if(users!=null){
		return users.get(0);
		}else{
			return null;
		}
		
	}
	public void add(tUser tuser) {
		// TODO Auto-generated method stub
		
		
		this.tuserDao.add(tuser);
		

	}

	public void delete(String userId) {
		// TODO Auto-generated method stub
		
		this.tuserDao.delete(userId);

	}

	public List<tUser> search(String userName) {
		// TODO Auto-generated method stub
		return this.tuserDao.search(userName);
	}
	
	public List<tUser> searchuserName(String userName){
		return this.tuserDao.searchuserName(userName);
	}
	
	public PageBean searchuserName(String userName,int pageSize,int page){
		final String hql = "from tUser where userName like " + "'%" + userName + "%'";        //查询语句
        int allRow = tuserDao.getAllRowCount(hql);    //总记录数
        System.out.println(allRow);
        int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int currentPage = PageBean.countCurrentPage(page);
        final int offset = PageBean.countOffset(pageSize, currentPage);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        
        List list = tuserDao.queryForPage(hql,offset, length);        //"一页"的记录
        
        //把分页信息保存到Bean中
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        System.out.println(allRow);
        return pageBean;
	}
	
	

	public void update(tUser tuser) {
		// TODO Auto-generated method stub
		
		this.tuserDao.update(tuser);

	}
	
	public PageBean searchAlltUser(int pageSize,int page){
		
		final String hql = "from tUser";        //查询语句
        int allRow = tuserDao.getAllRowCount(hql);    //总记录数
        System.out.println(allRow);
        int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int currentPage = PageBean.countCurrentPage(page);
        final int offset = PageBean.countOffset(pageSize, currentPage);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        
        List list = tuserDao.queryForPage(hql,offset, length);        //"一页"的记录
        
        //把分页信息保存到Bean中
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        System.out.println(allRow);
        return pageBean;
	}
	
	public List<tUser> searchAlltUser(){
		return this.tuserDao.searchAlltUser();
	}
	
	public List<tUser> searchById(String userId){
		
		return this.tuserDao.searchById(userId);
	}
	
	public List<tUser> searchByGrade(){
		return this.tuserDao.searchByGrade();
	}

	public tUserDao getTuserDao() {
		return tuserDao;
	}

	public void setTuserDao(tUserDao tuserDao) {
		this.tuserDao = tuserDao;
	}
	
}
